//
//  NetWorkingTool.m
//  block数据解析
//
//  Created by dllo on 15/4/20.
//  Copyright (c) 2015年 李小静. All rights reserved.
//

#import "NetWorkingTool.h"
#import "AFHTTPRequestOperationManager.h"

@implementation NetWorkingTool

+ (void)netWorkingToolStr:(NSString *)strURL block:(Block)myBlock
{
#pragma mark - 判断网络状态    AFN
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
                
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");
                
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
        
                NSString *strURLEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                NSURL *url = [NSURL URLWithString:strURLEncode];
                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
                [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                    
                    if (data == nil) {
                        NSLog(@"aaa");
                    }
                    
                    else
                    {
                    // 解析到的数据
                    id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                    myBlock(result);    // 调用传过来的block
                    }
                }];
                
                
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];

    
   
}

@end
